﻿using Pub;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DBMgr
{
    public class Program
    {
        static String server = "";
        static String user = "";
        static String pass = "";
        static String database = "";
        static void Main(string[] args)
        {
            //fun1();
            while (true)
            {
                try
                {
                    Console.WriteLine("server:");
                    server = Console.ReadLine();
                    Console.WriteLine("user:");
                    user = Console.ReadLine();
                    Console.WriteLine("pass:");
                    pass = Console.ReadLine();
                    Console.WriteLine("database:");
                    database = Console.ReadLine();
                    
                    Console.WriteLine("table:");
                    string table = Console.ReadLine();
                    Console.WriteLine("sql:");
                    string sql = Console.ReadLine();
                    fun1(table, sql);
                }
                catch (Exception ex) {
                    Console.Write(ex.ToString());
                }
                Console.WriteLine("Exit(y/n)?:");
                string cmd = Console.ReadLine();
                if (cmd == "y") break;
            }
        }

        public static void fun1(string sTabName, string strSQL)
        {
            Console.WriteLine(Environment.CurrentDirectory);
            AdoMSSQL ado1 = new AdoMSSQL(server, user, pass, database);

            DataSet ds1 = ado1.sel1(strSQL);
            Console.Write(strSQL + "  ---" + ado1.ds_count + "\n");
            if (!CDB.dsIsNull(ds1))
            {
                String file = Environment.CurrentDirectory + "/" + sTabName + ".sql";
                StringBuilder strSQL_Tem = new StringBuilder();
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds1.Tables[0].Rows[i];
                    strSQL_Tem.Append(CDB.getSQL_Insert(sTabName, dr, true, false) + "\n");
                }
                CFile.Write(file, strSQL_Tem.ToString());

            }
            else
            {
                Console.WriteLine("没有记录!");
            }
            
            //Console.WriteLine("finished! \npress enter key to exit");
            //Console.ReadKey();
        }

        public static void fun1() {
            Console.WriteLine(Environment.CurrentDirectory);
            AdoMSSQL ado1 = new AdoMSSQL(server, user, pass, database);
            string[] sTabNameS = CConfig.getPermite("sTabName").Split(','); //"Effect_Work";
            foreach (string sTabName in sTabNameS)
            {
                string strSQL = "SELECT * FROM " + sTabName;

                DataSet ds1 = ado1.sel1(strSQL);
                Console.Write(strSQL + "  ---" + ado1.ds_count + "\n");
                if (!CDB.dsIsNull(ds1))
                {
                    String file = Environment.CurrentDirectory + "/" + sTabName + ".sql";
                    StringBuilder strSQL_Tem = new StringBuilder();
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr = ds1.Tables[0].Rows[i];
                        strSQL_Tem.Append(CDB.getSQL_Insert(sTabName, dr, true, false) + "\n");
                    }
                    CFile.Write(file, strSQL_Tem.ToString());

                }
                else
                {
                    Console.WriteLine("没有记录!");
                }
            }
            //Console.WriteLine("finished! \npress enter key to exit");
            //Console.ReadKey();
        }
    }
}
